var Model = require("../../../model/Model");
var CardUtil = require("../../../util/CardUtil");
module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
    this.app = app;
    this.channelService = app.get("channelService");
};

Handler.prototype.prepare = function(msg, session, next) {
    var uid = session.uid;
    var roomId = session.get("roomId");
    var roomModel = Model.room[roomId];
    var channel = this.channelService.getChannel(roomId);
    var allPrepareFlag = true;
    for (var i = 0; i < roomModel.player.length; i++) {
        if (roomModel.player[i] && roomModel.player[i].uid === uid) {
            roomModel.player[i].prepare = true;
            channel.pushMessage("push", {data:{model:[{key:"model.room.player.["+i+"].prepare", value:true}]}});
        }
        if (!roomModel.player[i] || roomModel.player[i].prepare === false) {
            allPrepareFlag = false;
        } 
    }
    next(null, {ret:0});
    if (allPrepareFlag) {
        roomModel.status = 1;
        roomModel.card = CardUtil.genRndCard();
        for(var i = 0; i < roomModel.player.length; i++) {
            roomModel.player[i].card = roomModel.card.splice(0, 13);
            roomModel.player[i].goneCard = [];
            roomModel.player[i].winCard = [];
        }
        roomModel.speaker = 0;
        roomModel.speakerTime = new Date().getTime();
        channel.pushMessage("push", {data:{model:[{key:"model.room", value:roomModel}]}});
    }
};
Handler.prototype.walk = function(msg, session, next) {
    var uid = session.uid;
    var roomId = session.get("roomId");
    var roomModel = Model.room[roomId];
    var channel = this.channelService.getChannel(roomId);
    var walkIndex = msg.walkIndex;
    if (roomModel.player[roomModel.speaker].uid === uid) {
        roomModel.player[roomModel.speaker].goneCard.push(
            roomModel.player[roomModel.speaker].card.splice(walkIndex, 1)
        );
        roomModel.speaker = (roomModel.speaker + 1) % roomModel.player.length;
        roomModel.speakerTime = new Date().getTime();
        roomModel.player[roomModel.speaker].card.push(
            roomModel.player[i].card = roomModel.card.splice(0, 1)
        );
        channel.pushMessage("push", {data:{model:[{key:"model.room", value:roomModel}]}});
    }
    next(null, {ret:0});
};
Handler.prototype.eat = function(msg, session, next) {
    var uid = session.uid;
    var roomId = session.get("roomId");
    var roomModel = Model.room[roomId];
    var channel = this.channelService.getChannel(roomId);
    var useCard = msg.useCard;
    var myIndex = 0;
    for (var i = 0; i < roomModel.player.length; i++) {
        if (roomModel.player[i] && roomModel.player[i].uid === uid) {
            myIndex = i;
        }
    }
    roomModel.player[myIndex].winCard.push([
        roomModel.player[myIndex].card.splice(Math.max(useCard[0],useCard[1]), 1),
        roomModel.player[myIndex].card.splice(Math.min(useCard[0],useCard[1]), 1),
        roomModel.player[roomModel.lastSpeaker].goneCard.pop()
    ]);
    roomModel.lastSpeaker = null;
    roomModel.speaker = myIndex;
    roomModel.speakerTime = new Date().getTime();
    channel.pushMessage("push", {data:{model:[{key:"model.room", value:roomModel}]}});
    
    next(null, {ret:0});
};
Handler.prototype.peng = function(msg, session, next) {
    var uid = session.uid;
    var roomId = session.get("roomId");
    var roomModel = Model.room[roomId];
    var channel = this.channelService.getChannel(roomId);
    var useCard = msg.useCard;
    var myIndex = 0;
    for (var i = 0; i < roomModel.player.length; i++) {
        if (roomModel.player[i] && roomModel.player[i].uid === uid) {
            myIndex = i;
        }
    }
    roomModel.player[myIndex].winCard.push([
        roomModel.player[myIndex].card.splice(Math.max(useCard[0],useCard[1]), 1),
        roomModel.player[myIndex].card.splice(Math.min(useCard[0],useCard[1]), 1),
        roomModel.player[roomModel.lastSpeaker].goneCard.pop()
    ]);
    roomModel.lastSpeaker = null;
    roomModel.speaker = myIndex;
    roomModel.speakerTime = new Date().getTime();
    channel.pushMessage("push", {data:{model:[{key:"model.room", value:roomModel}]}});
    
    next(null, {ret:0});
};
Handler.prototype.win = function(msg, session, next) {
    next(null, {ret:0});
};
